home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / ranks.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  103 lines

  1. ;$Id: ranks.pro,v 1.6 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       RANKS
  8. ;
  9. ; PURPOSE:
  10. ;       This function computes the magnitude-based ranks of a sample 
  11. ;       population X. Elements of identical magnitude "ties" are ranked 
  12. ;       according to the mean of the ranks that would otherwise be assigned.
  13. ;       The result is a vector of ranks equal in length to X.
  14. ;
  15. ; CATEGORY:
  16. ;       Statistics.
  17. ;
  18. ; CALLING SEQUENCE:
  19. ;       Result = Ranks(X)
  20. ;
  21. ; INPUTS:
  22. ;       X:    An n-element vector of type integer, float or double.
  23. ;             The elements of this vector must be in ascending order
  24. ;             based on their magnitude.
  25. ;
  26. ; EXAMPLE:
  27. ;       Define an n-element sample population.
  28. ;         x = [-0.8, 0.1, -2.3, -0.6, 0.2, 1.1, -0.3, 0.6, -0.2, 1.1, -0.7, $
  29. ;              -0.2, 0.6, 0.4, -0.1, 1.1, -0.3, 0.3, -1.3, 1.1]
  30. ;
  31. ;       Allocate a two-column, n-row array to store the results.
  32. ;         array = fltarr(2, n_elements(x))
  33. ;
  34. ;       Sort the sample population and store in the 0th column of ARRAY.
  35. ;         array[0, *] = x[sort(x)]
  36. ;
  37. ;       Compute the ranks of the sorted sample population and store in the 
  38. ;       1st column of ARRAY.
  39. ;         array[1, *] = ranks[x[sort(x)]]
  40. ;
  41. ;       Display the sorted sample population and corresponding ranks with a 
  42. ;       two-decimal format.
  43. ;         print, array, format = '(2(5x, f5.2))'
  44. ;       
  45. ;       The result should be:
  46. ;         -2.30      1.00
  47. ;         -1.30      2.00
  48. ;         -0.80      3.00
  49. ;         -0.70      4.00
  50. ;         -0.60      5.00
  51. ;         -0.30      6.50
  52. ;         -0.30      6.50
  53. ;         -0.20      8.50
  54. ;         -0.20      8.50
  55. ;         -0.10     10.00
  56. ;          0.10     11.00
  57. ;          0.20     12.00
  58. ;          0.30     13.00
  59. ;          0.40     14.00
  60. ;          0.60     15.50
  61. ;          0.60     15.50
  62. ;          1.10     18.50
  63. ;          1.10     18.50
  64. ;          1.10     18.50
  65. ;          1.10     18.50
  66. ;
  67. ; REFERENCE:
  68. ;       PROBABILITY and STATISTICS for ENGINEERS and SCIENTISTS (3rd edition)
  69. ;       Ronald E. Walpole & Raymond H. Myers
  70. ;       ISBN 0-02-424170-9
  71. ;
  72. ; MODIFICATION HISTORY:
  73. ;       Written by:  GGS, RSI, November 1994
  74. ;-
  75.  
  76. function ranks, x
  77.  
  78.   on_error, 2
  79.  
  80.   nx = n_elements(x)
  81.   r_vec = [0.0, x] 
  82.   k = 1L
  83.   while(k lt nx) do begin
  84.     if(r_vec[k+1] ne r_vec[k]) then begin
  85.       r_vec[k] = k
  86.       k = k+1
  87.     endif else begin
  88.       for kt = k+1, nx do $
  89.         if (r_vec[kt] ne r_vec[k]) then goto, midrank
  90.       kt = nx + 1
  91.       midrank:
  92.       rank = 0.5 * (k + kt - 1)
  93.       r_vec[k:kt-1] = rank
  94.       t = kt - k
  95.       k = kt
  96.     endelse
  97.   endwhile
  98.   if(k eq nx) then r_vec[nx] = nx
  99.  
  100.   return, r_vec[1:*]
  101.  
  102. end
  103.